﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NuGet.Common;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using WMSUIMVC.Aubit.Procurement;
using XuanKeZz.Application.HTTP;

namespace WMSUIMVC.Controllers
{

    /// <summary>
    /// 采购管理
    /// </summary>
    public class ProcurementController : Controller
    {

        private readonly IConfiguration configuration;


        public ProcurementController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }


        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加采购信息
        /// </summary>
        /// <returns></returns>
        public IActionResult AddProcurement()
        {
            return View();
        }


        /// <summary>
        /// 显示采购信息
        /// </summary>
        /// <returns></returns>
        public IActionResult GetProcurementList()
        {
            return View();
        }

        /// <summary>
        /// 修改采购信息
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdProcurement()
        {
            return View();
        }

        /// <summary>
        /// 添加采购
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IActionResult> AddProcurementList(AddProcurementDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"]+"ywx/api/Procurement/AddRrocurement";
            string list = JsonConvert.SerializeObject(obj);
            string res = await client.PostAsync(url, list);
            return Json(res);
        }


        /// <summary>
        /// 显示采购的数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetProcurement(GetProcurementDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywd/api/Procurement/GetRrocurement?ProcurementCode=" + obj.ProcurementCode + "&ProductsCode=" + obj.ProductsCode + "&ProductsName=" + obj.ProductsName + "&ProcureState=" + obj.ProcureState + "&pindex=" + obj.Pindex + "&psize=" + obj.Psize;
            string res = await client.GetAsync(url);
            return Json(res);
        }


        /// <summary>
        /// 显示产品的数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetProductList(GetProductsDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywd/api/Procurement/GetProductList?productsCode=" + obj.ProductsCode + "&productsName=" + obj.ProductsName + "&pageindex=" + obj.Pindex + "&pagesize=" + obj.Psize;
            string res = await client.GetAsync(url);
            return Json(res);
        }

        /// <summary>
        /// 显示采购的查看详情
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetProcurementDetail(GetProcurementDetailDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywd/api/Procurement/GetRrocurementDetail?ProcureProductId=" + obj.ProcureProductId;
            string res = await client.GetAsync(url);
            return Json(res);
        }

        /// <summary>
        /// 操作删除采购
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<IActionResult> DelProcurement(DelProcurementDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywx/api/Procurement/DelRrocurement";
            //转成集合
            var list = "{\"procureProductId\":" + JsonConvert.SerializeObject(obj.ProcureProductId) + "}";
            //将集合转成json
            var res = await client.PutAsync(url, list);
            return Json(res);
        }

        /// <summary>
        /// 反填采购
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<IActionResult> FanProcurement(FanProcurementDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywd/api/Procurement/FanProduct?ProductsID=" + obj.ProductsID;
            var res = await client.GetAsync(url);
            return Json(res);
        }

        /// <summary>
        /// 显示选中的产品
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<IActionResult> CheckProduct(CheckProductsDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywd/api/Procurement/GetProductByid?id=" + obj.ProductsID;
            var res = await client.GetAsync(url);
            return Json(res);
        }

        /// <summary>
        /// 修改采购
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IActionResult> UpdProcurementList(AddProcurementDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywx/api/Procurement/UpdRrocurement";
            string list = JsonConvert.SerializeObject(obj);
            string res = await client.PostAsync(url, list);
            return Json(res);
        }


        /// 添加采购退货
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IActionResult> AddProcureRefund(AddRefundDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywx/api/Refund/AddRefund";
            string list = JsonConvert.SerializeObject(obj);
            string res = await client.PostAsync(url, list);
            return Json(res);
        }

        /// <summary>
        /// 显示操作列的退货
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetRefundDetail(GetRefundDetail obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywd/api/Procurement/GetRefundDetail?ProcureProductId=" + obj.ProcureProductId;
            string res = await client.GetAsync(url);
            return Json(res);
        }


        /// <summary>
        /// 显示入库的下拉框
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetStorageList(GetStorageDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywd/api/Procurement/GetStorehouseList?StoreCode=" + obj.StoreCode + "&StoreName=" + obj.StoreName + "&pageIndex=" + obj.pageIndex + "&PageSize=" + obj.PageSize+"&wid=" + obj.wid;
            string res = await client.GetAsync(url);
            return Json(res);
        }

        /// <summary>
        /// 添加入库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IActionResult> AddStorages(AddStorageDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywx/api/ProcurementStorage/AddRrocurement";
            string list = JsonConvert.SerializeObject(obj);
            string res = await client.PostAsync(url, list);
            return Json(res);
        }

        /// <summary>
        /// 修改入账
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IActionResult> UpdRecorded(AddRecordedDto obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = configuration["Url"] + "ywx/api/Procurement/UpdRecorded?procureProductId=" + obj.procureProductId + "&IsRecorded="+obj.IsRecorded;
            string list = JsonConvert.SerializeObject(obj);
            string res = await client.PostAsync(url, list);
            return Json(res);
        }


    }
}
